<?php
/**
 * 产品分类
 */
namespace Admin\Controller;
use Think\Controller;
class ProductCatController extends InitController {

	//分类列表
	public function categoryList() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];

		$type = I('type');

		$where = array();

		$where['parent_id'] = 0;

		if($type != 'all' and !empty($type)){
			$where['type'] = $type;
		}

		$where['status'] = 1;

		$categoryModel = D('ProductCategory');
		$total = $categoryModel->getProductCategoryNum($where);
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';
		$list = $categoryModel->getProductCategoryList($where,'taxis desc,id asc',$Page->firstRow.','.$Page->listRows,1);
		
		// 查询子类
		if(!empty($list)){
			foreach($list as $k=>$v){
				$list[$k]['sub'] = $categoryModel->where(array('parent_id'=>$v['id']))->order("taxis desc,id asc")->select();
			}
		}
		
		$this->assign('list',$list);
		$this->assign('pager',$show);
		$this->assign('type',$type);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Product/categoryList');
	}
	
	// 添加分类
	public function categoryAdd() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		$pid = I('pid',0,'intval');
		
		$this->assign('pid',$pid);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Product/categoryAdd');
	}
	
	// 添加产品分类
	public function do_categoryAdd() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$title = I('title','','strip_tags,htmlspecialchars,trim');
		if (empty($title)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入产品分类标题!','field'=>''));
		}
		$pid = I('pid',0,'intval');
		$status = I('status',0,'intval');
		$taxis = I('taxis',100,'intval');
		
		$data['name'] = $title;
		$categoryModel = M('ProductCategory');

		$data['status'] = $status;
		$data['taxis'] = $taxis;
		$data['creatime'] = NOW_TIME;
		$data['parent_id'] = $pid;
		
		if ($categoryModel->add($data)) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 编辑产品分类
	public function categoryEdit() {
		$id = I('id',0,'intval');
		if (!id) {
			$this->error('参数错误');
		}
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$categoryModel = D('ProductCategory')->getProductCategoryById($id);
		
		$this->assign('navi',$categoryModel);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Product/categoryEdit');
	}
	
	// 编辑产品操作
	public function do_categoryEdit() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');

		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		$title = I('title','','strip_tags,htmlspecialchars,trim');
		if (empty($title)) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'请输入产品分类标题!','field'=>''));
		}

		$status = I('status',0,'intval');
		$taxis = I('taxis',100,'intval');
		
		$data['name'] = $title;
		$data['status'] = $status;
		$data['taxis'] = $taxis;
		
		if (M('ProductCategory')->where(array('id'=>$id))->save($data) !== false) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 删除产品分类操作
	public function categoryDel() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');

		if (!id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}

		if (M('ProductCategory')->where(array('id'=>$id))->save(array('status'=>'-1'))) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}

}
